package cn.sc.summer.exception.hodler;

import cn.sc.summer.token.po.UserDetailX;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

/**
 * 以静态变量保存租户信息
 *
 * @author a-xin
 */
@Component
public class UserContextHolder implements DisposableBean {

    /**
     * 用户信息
     */
    private static final ThreadLocal<UserDetailX> USER_INFO = new ThreadLocal<>();

    /**
     * 获取用户信息
     */
    public static UserDetailX getUserInfo() {
        return USER_INFO.get();
    }

    /**
     * 存储用户信息
     */
    public static void setUserInfo(UserDetailX tenantInfo) {
        USER_INFO.set(tenantInfo);
    }

    /**
     * 清除用户信息
     */
    public static void clearUserInfo() {
        USER_INFO.remove();
    }

    /**
     * Invoked by the containing {@code BeanFactory} on destruction of a bean.
     */
    @Override
    public void destroy() {
        USER_INFO.remove();
    }

}
